package com.queming.core.controller;

import com.queming.core.dto.FileUploadRequest;
import com.queming.core.mapper.FileMapper;
import com.queming.core.mapper.FileUploadMapper;
import com.queming.core.service.file.FileService;
import com.queming.core.vo.FileUploadVo;
import com.queming.core.vo.FileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author chencidiao
 * @date 2022/05/04 13:37
 * 必须是管理员才可以访问文件接口
 **/
@RestController
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping("/file")
public class FileController {

    private FileService fileService;
    @Autowired
    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }

    private FileUploadMapper mapper;
    @Autowired
    public void setMapper(FileUploadMapper mapper) {
        this.mapper = mapper;
    }

    private FileMapper fileMapper;
    @Autowired
    public void setMapper(FileMapper mapper) {
        this.fileMapper = mapper;
    }

    @PostMapping("/upload_init")
    public FileUploadVo initUpload(@Validated @RequestBody FileUploadRequest fileUploadRequest) throws IOException {
        return mapper.toVo(fileService.initUpload(fileUploadRequest));
    }

    @PostMapping("/{id}/upload_finish")
    public FileVo finishUpload(@PathVariable String id){
        return fileMapper.toVo(fileService.finishUpload(id));
    }
}
